package com.example.demo.utils;

import org.springframework.util.Assert;
import org.springframework.util.DigestUtils;

import java.util.UUID;

public class SecurityUtils {
   public static String encrypt(String password){
       Assert.notNull(password,"password 不能为null");
        String salt = UUID.randomUUID().toString().replace("-","");
        String secretString = DigestUtils.md5DigestAsHex((password+salt).getBytes());

        //数据库存储: 盐值+ 密文
        String sqlPassword = salt+secretString;
        return sqlPassword;
    }

    /**
     * 验证
     */

    public static boolean verify(String password,String sqlPassword){
        if(sqlPassword ==null || sqlPassword.length()!=64){
            return false;
        }
        String salt = sqlPassword.substring(0,32);
        String secretString = DigestUtils.md5DigestAsHex((password+salt).getBytes());
        String result = salt+secretString;
        return result.equals(sqlPassword);

    }
}
